/**
 * Copyright 2016-2018 mobaas.com
 */
package com.mobaas.devops.monitor;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.ecs.model.v20140526.DescribeInstanceMonitorDataRequest;
import com.aliyuncs.ecs.model.v20140526.DescribeInstanceMonitorDataResponse;
import com.aliyuncs.ecs.model.v20140526.DescribeInstanceMonitorDataResponse.InstanceMonitorData;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.mobaas.devops.HostMonitor;
import com.mobaas.devops.model.HostStat;

/**
 * 阿里云ECS主机监控
 * @author billy zhang
 *
 */
public class AliyunHostMonitor implements HostMonitor {

	//private static final Logger log = LoggerFactory.getLogger(AliyunHostMonitor.class);
	
	private static final SimpleDateFormat iso8601fmtIn = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
	private static final SimpleDateFormat iso8601fmtOut = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
	
	@Override
	public List<HostStat> getHostStat(Map<String, Object> config) {
		
		String regionId = (String)config.get("regionId");
		String accessKeyId = (String)config.get("accessKeyId");
		String accessKeySecret = (String)config.get("accessKeySecret");

		int hostId = (Integer)config.get("hostId");
		String instanceId = (String)config.get("instanceId");
		
		IClientProfile profile  = DefaultProfile.getProfile(regionId, accessKeyId,  accessKeySecret);
		IAcsClient client = new DefaultAcsClient(profile);
		 
		DescribeInstanceMonitorDataRequest req = new DescribeInstanceMonitorDataRequest();
		req.setInstanceId(instanceId);
		req.setPeriod(60);
		
		Calendar cal = Calendar.getInstance();
		int zoneOffset = cal.get(Calendar.ZONE_OFFSET);
		cal.add(Calendar.MILLISECOND, 0-zoneOffset);
		Date time = cal.getTime();
		cal.add(Calendar.MINUTE, -3);
		req.setStartTime( iso8601fmtIn.format(cal.getTime()) + "Z" );
		req.setEndTime( iso8601fmtIn.format(time) + "Z" );
		
		try {
			List<HostStat> statsList = new ArrayList<HostStat>();
			
			DescribeInstanceMonitorDataResponse resp = client.getAcsResponse(req);
			List<InstanceMonitorData> list2 = resp.getMonitorData();
			for (InstanceMonitorData data : list2) {

				try {
					cal = Calendar.getInstance();
					cal.setTime( iso8601fmtOut.parse(data.getTimeStamp()) );
					cal.add(Calendar.MILLISECOND, zoneOffset);
					time = cal.getTime();
					
					HostStat stats = new HostStat();
					stats.setHostId(hostId);
					stats.setAddTime(time);
					
					if (data.getCPU() != null)
						stats.setCPU(data.getCPU().intValue());
					if (data.getInternetRX() != null)
						stats.setNetRx(data.getInternetRX().intValue());
					if (data.getInternetTX() != null)
						stats.setNetTx(data.getInternetTX().intValue());
					if (data.getIOPSRead() != null)
						stats.setIOPSRead(data.getIOPSRead());
					if (data.getIOPSWrite() != null)
						stats.setIOPSWrite(data.getIOPSWrite());
					if (data.getBPSRead() != null)
						stats.setBPSRead(data.getBPSRead());
					if (data.getBPSWrite() != null)
						stats.setBPSWrite(data.getBPSWrite());
					
					statsList.add(stats);
				} catch (ParseException ex) {
					//log.error(ex.getMessage(), ex);
					ex.printStackTrace();
				}
			}
			
			return statsList;
			
		} catch (ServerException ex) {
			//log.error(ex.getMessage(), ex);
			ex.printStackTrace();
		} catch (ClientException ex) {
			//log.error(ex.getMessage(), ex);
			ex.printStackTrace();
		} 
		
		return null;
	}
}
